<template>
  <div class="douban-anime-search">
    <div class="search-input-container">
      <input
        type="text"
        v-model="searchQuery"
        placeholder="输入动漫名称进行搜索..."
        class="search-input"
        @keyup.enter="searchAnime"
      />
      <button 
        class="search-button"
        :disabled="!searchQuery || isSearching"
        @click="searchAnime"
      >
        <span v-if="isSearching">搜索中...</span>
        <span v-else>搜索</span>
      </button>
    </div>
    
    <div v-if="isSearching" class="loading">
      <p>正在搜索中...</p>
    </div>
    
    <div v-else-if="searchResults.length > 0" class="search-results">
      <StandardAnimeGrid
        :animes="searchResults"
        :show-vote-button="false"
        :show-nominate-button="true"
        :show-favorite-button="false"
        :show-votes="false"
        :show-favorites-count="false"
        :show-pagination="false"
        @show-detail="handleShowDetail"
        @nominate="handleNominate"
      />
    </div>
    
    <div v-else-if="searchQuery" class="no-results">
      <p>未找到相关动漫，请尝试其他关键词</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import StandardAnimeGrid from './StandardAnimeGrid.vue'
import type { Anime } from '../types/anime'
import { getDoubanAnimeSearch } from '../services/animeService'
import toastService from '../services/toastService'

// 定义props
interface Props {
  onNominate: (anime: Anime) => void
  onShowDetail: (anime: Anime) => void
  onFavorite: (animeId: number) => void
}

defineProps<Props>()

// 定义emits
const emit = defineEmits<{
  (e: 'nominate', anime: Anime): void
  (e: 'show-detail', anime: Anime): void
  (e: 'favorite', animeId: number): void
}>()

// 组件状态
const searchQuery = ref('')
const isSearching = ref(false)
const searchResults = ref<Anime[]>([])



// 搜索动漫
const searchAnime = async () => {
  if (!searchQuery.value.trim()) {
    toastService.info('请输入动漫名称进行搜索')
    return
  }
  
  isSearching.value = true
  
  try {
    // 使用animeService中的getDoubanAnimeSearch函数
    const result = await getDoubanAnimeSearch(searchQuery.value.trim());
    
    // 设置搜索结果
    searchResults.value = result.data;
    
    toastService.success(`找到 ${searchResults.value.length} 个相关动漫`);
  } catch (error) {
    console.error('豆瓣动漫搜索失败:', error)
    toastService.error('搜索失败，请稍后再试')
    searchResults.value = []
  } finally {
    isSearching.value = false
  }
}


// 处理提名
const handleNominate = (anime: Anime) => {
  emit('nominate', anime)
}

// 处理显示详情
const handleShowDetail = (anime: Anime) => {
  emit('show-detail', anime)
}

// handleFavorite function removed as it's not being used
</script>

<style scoped>
.douban-anime-search {
  width: 100%;
}

.search-input-container {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  max-width: 700px;
  margin-left: auto;
  margin-right: auto;
}

.search-input {
  flex: 1;
  padding: 14px 16px;
  border: 1px solid #E5E5EA;
  border-radius: 12px;
  font-size: 1rem;
  background-color: #F9F9F9;
  color: #1C1C1E;
  transition: all 0.2s ease;
}

.search-input:focus {
  outline: none;
  border-color: #007AFF;
  background-color: white;
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.2);
}

.search-button {
  padding: 14px 24px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.search-button:hover:not(:disabled) {
  background-color: #0062CC;
  transform: translateY(-1px);
}

.search-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.loading, .no-results {
  text-align: center;
  padding: 40px 0;
  color: #6C6C70;
  font-size: 1rem;
}

.search-results {
  margin-top: 24px;
}

/* 覆盖StandardAnimeGrid的样式，确保搜索结果显示为一行三个 */
:deep(.anime-grid) {
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

@media (max-width: 992px) {
  :deep(.anime-grid) {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>